//
//  LLT_NewsDetailViewController_LXH.m
//  CoderLLT
//
//  Created by lanou3g on 15/12/10.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_NewsDetailViewController_LXH.h"
#import <AVOSCloud/AVOSCloud.h>
#import "UIViewController+LLT_LoginAndRegist_LXM.h"
#import "AppDelegate.h"



@interface LLT_NewsDetailViewController_LXH ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *newsWebView;
@property (weak, nonatomic) IBOutlet UIButton *collectButton;

@property (nonatomic, assign) BOOL isCollect;

@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation LLT_NewsDetailViewController_LXH

#pragma mark --- dealloc
-(void)dealloc
{
    self.data = nil;
}



#pragma mark --- 懒加载

-(NSMutableArray *)data
{
    if (!_data) {
        _data = [NSMutableArray new];
    }
    return _data;
}


#pragma mark --- 加载视图

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNewsWebView];
    
//    [self changeButton];
    
//    [self containsTheNews];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [self containsTheNews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- 设置视图

/**
 * 改变按钮的样式
 */
-(void)changeButton
{
    for (LLT_NewsModel *news in self.data) {
        if ([news.title isEqualToString:self.newsModel.title]) {
            self.isCollect = YES;
        }else{
            self.isCollect = NO;
        }
    }
    if(!self.isCollect) {
        [self.collectButton setImage:[UIImage imageNamed:@"cshoucang"] forState:UIControlStateNormal];
    }else{
        [self.collectButton setImage:[UIImage imageNamed:@"cshoucang_holder"] forState:UIControlStateNormal];
    }
    
    [self.collectButton addTarget:self action:@selector(collectButton:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 设置view里面各种视图

/**
 *  设置webview
 */
-(void)makeNewsWebView{
    
    NSString *s = kNewsDetailURL(self.urlString);
    NSString *html = [self removeHTMLAdvertisementByURLString:s];
    [self.newsWebView loadHTMLString:html baseURL:nil];
    self.newsWebView.backgroundColor = [UIColor clearColor];
}

/**
 * 去除html中的广告
 *
 * @return 返回值为string类型的 html 标签组
 *
 * 刘晓萌 添加
 */
-(NSString *)removeHTMLAdvertisementByURLString:(NSString *) urlString{
    NSString *dataString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:@"<div class=\"maxjia-ad\">"];
    [arr addObject:@"<div class=\"app_logo\">"];
    [arr addObject:@"<img src=\"http://news.maxjia.com/css/img/app_logo.png\"/>"];
    [arr addObject:@"<p class=\"app_name\">Max+</p>"];
    [arr addObject:@"<p class=\"app_des\">国内最专业的游戏数据平台</p>"];
    [arr addObject:@"<div onclick=\"window.location.href='http://a.app.qq.com/o/simple.jsp?pkgname=com.dotamax.app'\" class=\"download_btn\">立即下载</div>"];
    [arr addObject:@"<div class=\"maxjia-ad-2\">"];
    [arr addObject:@"<a onclick=\"window.location.href='http://a.app.qq.com/o/simple.jsp?pkgname=com.dotamax.app'\">点击下载 Max+ 了解更多</a>"];
    [arr addObject:@"<p style=\"text-align:center\">"];
    [arr addObject:@"<a onclick=\"window.location.href='http://a.app.qq.com/o/simple.jsp?pkgname=com.dotamax.app'\" style=\"cursor: pointer;\" href=\"http://dotamax.com/app/\">"];
     [arr addObject:@"<img src=\"http://cdn.maxjia.com/image/share/qr_in_one.png\" style=\"width:60%;margin-top:10px;\">"];
    [arr addObject:@"<br /><span style=\"font-size:11px;color:#aaa\">Max+ 最好用的LOL APP</span>"];
    [arr addObject:@"<div class=\"artWrap\" id=\"1974\">"];
    [arr addObject:@"<img src=\"http://news.maxjia.com/static/css/img/app_logo.png\"/>"];
    for (NSString *s in arr) {
        dataString =  [dataString stringByReplacingOccurrencesOfString:s withString:@""];
    }
    return dataString;
}  

/**
 * 设置评论区tableView
 */
-(void)makeCommentsTableView{
    
    
//    [self.commentsTableView registerNib:nil forCellReuseIdentifier:@"cell"];
    
}


#pragma mark - 实现按钮方法

/**
 * 点击返回按钮
 */
- (IBAction)didClickReturnButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 * 点击收藏按钮
 */
-(void)collectButton:(UIButton *)sender{
    if (!self.isCollect)
    {
        AVObject *news = [AVObject objectWithClassName:@"LLT_NewsModel"];
        news[@"date"] = self.newsModel.date;
        news[@"descriptionNews"] = self.newsModel.descriptionNews;
        news[@"cuserName"] = [AVUser currentUser].mobilePhoneNumber;
        news[@"title"] = self.newsModel.title;
        news[@"newsUrl"] = self.urlString;
        news[@"imgs"] = self.newsModel.imgs;
        news[@"newsid"] = self.newsModel.newsid;
        [news saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                [self showAlert:@"提 示" andAlertString:@"收藏成功"];
                [self.collectButton setImage:[UIImage imageNamed:@"cshoucang_holder"] forState:UIControlStateNormal];
                self.isCollect = YES;
            }else{
                //NSLog(@"%@",error);
            }
        }];
    }
    else
    {
        
        AVQuery *query = [AVQuery queryWithClassName:@"LLT_NewsModel"];
        [query whereKey:@"cuserName" equalTo:[AVUser currentUser].mobilePhoneNumber];
        [query whereKey:@"title" equalTo:self.newsModel.title];
        [query whereKey:@"date" equalTo:self.newsModel.date];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                for (AVObject *object in objects) {
                    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        if (succeeded) {
                            [self showAlert:@"提 示" andAlertString:@"取消收藏成功"];
                            [self.collectButton setImage:[UIImage imageNamed:@"cshoucang"] forState:UIControlStateNormal];
                            self.isCollect = NO;
                        }
                    }];
                }
            
            } else {
                // 输出错误信息
                //NSLog(@"Error: %@ %@", error, [error userInfo]);
            }
        }];
    }
}


#pragma mark --- 实现别的方法

/**
 * 判断是否包含此新闻
 */
-(void)containsTheNews
{
    
    AVQuery *query = [AVQuery queryWithClassName:@"LLT_NewsModel"];
    if ([AVUser currentUser].mobilePhoneNumber.length <= 0) {
        self.collectButton.hidden = YES;
        return;
    }
    [query whereKey:@"cuserName" equalTo:[AVUser currentUser].mobilePhoneNumber];
    [query whereKey:@"title" equalTo:self.newsModel.title];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (int i = 0; i < objects.count; i ++) {
                NSDictionary *dic = objects[i][@"localData"];
                LLT_NewsModel *model = [[LLT_NewsModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.data addObject:model];
            }
            [self performSelectorOnMainThread:@selector(changeButton) withObject:nil waitUntilDone:NO];
        }else{
            //NSLog(@"%@",error);
        }
    }];
//    //NSLog(@"contains the news self.data : %@",self.data);
}



/**
 * 电池栏 文字变白
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
